﻿var strUrl = document.location.href.toLowerCase();

$(function () {
    $(window).bind("load resize", function () {
        var topHigh = $("header").outerHeight();
        var botHigh = $("footer").outerHeight();
        var height = ((this.window.innerHeight > 0) ? this.window.innerHeight : this.screen.height) - 1;
        height = height - topHigh - botHigh+2;
        if (height < 1) height = 1;
        if (height > 0) {
            $("#mainWarp").css("min-height", (height) + "px");
        }       
    });

});

function amAlert(content, title) {
    if (title) {
        $('#my-alert').find(".am-modal-hd").html(title);
    }
    $('#my-alert').find(".am-modal-bd").html(content);
    var $alert = $('#my-alert').modal();
}

function amAlertFun(content, fun) {
    $('#my-alert').find(".am-modal-bd").html(content);
    var $alert = $('#my-alert').modal();

    $('#my-alert').on('closed.modal.amui', fun);
}

function amLogin() {   
    var $popup = $('#my-popup').modal();
}

function amLoading(title) {
    if (title) {
        $('#my-modal-loading').find(".am-modal-hd").html(title);
    }

    var $loading = $('#my-modal-loading').modal();
}

function amLoadingHide() {
    $('#my-modal-loading').modal('close');
}


//控件取值
function getValue(content) {
    var params = new Object;

    $("." + content).find("input,select").each(function () {
        params[$(this).attr("name")] = $(this).val();
    });
    return JSON.stringify(params);
}

//控件赋值
function setValue(params) {
    $.each(params, function (index, value) {
        $("#" + index).val(value);
    });
}


//url获取参数
function GetQueryString(name) {

    var result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));

    if (result == null || result.length < 1) {
        return "";
    }
    return result[1];
}


var QueryData = function (url, cmd) {
    this.Data["AjaxRequest"] = new Object();
    this.Data["AjaxRequest"]["URL"] = url;
    this.Data["AjaxRequest"]["Command"] = cmd;
    this.URL = url;
    this.Command = cmd;
}
QueryData.prototype = {
    URL: "",
    Command: "",
    Data: new Object(),
    SetQueryData: function (iscTag) {
        var data = this.Data[iscTag];
        if (typeof (data) == "undefined") {
            data = this.Data[iscTag] = new Object();
        }
        $("[isctag='" + iscTag + "']").each(function () {
            switch (this.tagName) {
                case "SPAN":
                    data[$(this).attr("id")] = $(this).text();
                    break;
                case "SELECT":
                    data[$(this).attr("id")] = $(this).val();
                    break;
                case "INPUT":
                    switch ($(this).attr("type")) {
                        case "checkbox":
                        case "radio":
                            if (this.checked == true) {
                                data[$(this).attr("id")] = $(this).val();
                            }
                            break;
                        default:
                            data[$(this).attr("id")] = $(this).val();
                            break;
                    }
                    break;
                default:
                    data[$(this).attr("id")] = $(this).text();
                    break;
            }

        });
        this.Data[iscTag] = data;

    },
    SetData: function (key, value) {
        var data = this.Data[key];
        if (typeof (data) == "undefined") {
            data = this.Data[key] = new Object();
        }
        data[key] = value;
        this.Data[key] = data;
    },
    setControlValue: function (params) {
        $.each(params, function (index, value) {
            switch ($("[id*='" + index + "']")[0].tagName) {
                case "SPAN":
                    $("[id*='" + index + "']").html(value);
                    break;
                default:
                    $("[id*='" + index + "']").val(value);
                    break;
            }
        });
    }

}



jQuery.extend({
    /** * @see 将javascript数据类型转换为json字符串 * @param 待转换对象,支持object,array,string,function,number,boolean,regexp * @return 返回json字符串 */
    toJSON: function (object) {
        var type = typeof object;
        if ('object' == type) {
            if (Array == object.constructor) type = 'array';
            else if (RegExp == object.constructor) type = 'regexp';
            else type = 'object';
        }
        switch (type) {
            case 'undefined':
            case 'unknown':
                return;
                break;
            case 'function':
            case 'boolean':
            case 'regexp':
                return object.toString();
                break;
            case 'number':
                return isFinite(object) ? object.toString() : 'null';
                break;
            case 'string':
                return '"' + object.replace(/(\\|\")/g, "\\$1").replace(/\n|\r|\t/g, function () {
                    var a = arguments[0];
                    return (a == '\n') ? '\\n' : (a == '\r') ? '\\r' : (a == '\t') ? '\\t' : ""
                }) + '"';
                break;
            case 'object':
                if (object === null) return 'null';
                var results = [];
                for (var property in object) {
                    var value = jQuery.toJSON(object[property]);
                    if (value !== undefined) results.push(jQuery.toJSON(property) + ':' + value);
                }
                return '{' + results.join(',') + '}';
                break;
            case 'array':
                var results = [];
                for (var i = 0; i < object.length; i++) {
                    var value = jQuery.toJSON(object[i]);
                    if (value !== undefined) results.push(value);
                }
                return '[' + results.join(',') + ']';
                break;
        }
    }
});


function checkAjaxResult(resultData) {

    if (resultData["ApplicationError"] == "True") {
        location.href = "/Pages/Error.aspx";
    }
    return true;
}

//写cookies 

function setCookie(name, value) {
    var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

//读取cookies 
function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");

    if (arr = document.cookie.match(reg))

        return unescape(arr[2]);
    else
        return "";
}

//删除cookies 
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null)
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}

var commonJs = {
    showInfoTips: function (_tipsStr) {
        var self = this;

        var info_tips = $("#info_tips_outer");
        info_tips.html("<div style='line-height:2em;'><span>" + _tipsStr + "</span></div>");
        info_tips.css({ "-moz-transform": "translate(0,0)", "-webkit-transform": "translate(0,0)", "transform": "translate(0,0)", "opacity": 1, "display": "block" });
        clearTimeout(infoTipsTimer);
        var infoTipsTimer = setTimeout(function () {
            self.hideInfoTips();
        }, 2500);
    },

    hideInfoTips: function () {
        var info_tips = $("#info_tips_outer");
        info_tips.css({ "-moz-transform": "translate(0,-10em)", "-webkit-transform": "translate(0,-10em)", "transform": "translate(0,-10em)", "opacity": 0 });
    },


    /*显示加载框*/
    showLoadTips: function (text) {
        $("#load_area").find("span").html(text);
        $("#load_area").show();
    },

    /*隐藏加载框*/
    hideLoadTips: function () {
        $("#load_area").hide();
    }

}



function login()
{
    if (!$('#fmLogin').data('amui.validator').isFormValid()) {
        return false;
    }

    var data = $("#fmLogin").serialize();
    $.ajax({
        type: "POST",
        url: "../handler/RegisterHandler.ashx",
        dataType: "json",
        data: data,
        success: function (result) {
            switch (result["state"]) {
                case "success":
                    commonJs.showInfoTips("登录成功！");
                    setTimeout(function () { window.location.href = location.href; }, 500);                    
                    break;
                case "norecord":
                    commonJs.showInfoTips("用户名或密码不对！");
                    break;
                case "error":
                    alert(result["msg"]);
                    break;
                default:
                    break;
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {  //#3这个error函数调试时非常有用，如果解析不正确，将会弹出错误框
            alert(XMLHttpRequest.status);
            alert(XMLHttpRequest.readyState);
            alert(textStatus); // paser error;
        },
        beforeSend: function () {
            $("#btnLogin").attr("disabled", "disabled");
        },
        complete: function () {
            $("#btnLogin").removeAttr("disabled");
        }
    });
}

function logout() {
    $.ajax({
        type: "POST",
        url: "../handler/RegisterHandler.ashx",
        dataType: "json",
        data: "act=logout",
        success: function (result) {
            switch (result["state"]) {
                case "success":                    
                    commonJs.showInfoTips("已注销！");
                    setTimeout(function () { window.location.href = location.href; }, 500);
                    break;
                case "error":
                    alert(result["msg"]);
                    break;
                default:
                    break;
            }
        }
    });
}

function ConvertTime(date) {
    var da = new Date(parseInt(date.replace("/Date(", "").replace(")/", "").split("+")[0]));
    return da.getFullYear() + "-" + (da.getMonth() + 1) + "-" + da.getDate() + " " + da.getHours() + ":" + da.getSeconds() + ":" + da.getMinutes();
}

function ConvertTimeNoHour(date) {
    if (date == undefined || date == "")
    {
        return "";
    }
    else
    {
        var da = new Date(parseInt(date.replace("/Date(", "").replace(")/", "").split("+")[0]));
        return da.getFullYear() + "-" + (da.getMonth() + 1) + "-" + da.getDate();
    }
    
}